package com.org.beauty.salon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.org.beauty.salon.entity.User;
import com.org.beauty.salon.mapper.UserMapper;
import com.org.beauty.salon.request.QueryByConditionRequest;
import com.org.beauty.salon.service.IUserService;
import com.org.beauty.salon.utils.CommonUtils;
import com.org.beauty.salon.utils.CopyUtil;
import com.org.beauty.salon.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author zhangyanbin
 * @since 2020-07-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 功能描述: 根据用户ID查询用户ID
     *
     * @author zhangyanbin
     * @date 2020/8/26 15:32
     **/

    @Override
    public User getByUserId(Long userId) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", userId).eq("delete_statue", 1));
        return user;
    }

    /**
     * 功能描述: 根据用户手机号查询用户信息
     *
     * @author zhangyanbin
     * @date 2020/8/26 15:47
     **/
    @Override
    public User getByPhone(String phone) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("phone", phone).eq("delete_statue", 1));
        return user;
    }

    /**
     * 功能描述: TODO
     *
     * @author zhangyanbin
     * @date 2020/8/26 16:57
     **/
    @Override
    public PageInfo<UserVo> listByCondition(@Nullable QueryByConditionRequest request) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_statue", 1);
        if(CommonUtils.isNotNull(request.getUserId())){
            queryWrapper.eq("user_id", request.getUserId());
        }
        if(CommonUtils.isNotNull(request.getName())){
            queryWrapper.eq("name", request.getName());
        }
        if(CommonUtils.isNotNull(request.getPhone())){
            queryWrapper.eq("phone", request.getPhone());
        }
        if(CommonUtils.isNotNull(request.getGrade())){
            queryWrapper.eq("grade", request.getGrade());
        }
        if(CommonUtils.isNotNull(request.getBeginDate()) && CommonUtils.isNotNull(request.getEndDate())){
            queryWrapper.ge("create_time", request.getBeginDate());
            queryWrapper.le("create_time", request.getEndDate());
        }
        PageInfo<UserVo> userPageInfo = PageHelper.startPage(request.getPageIndex(), request.getPageSize())
                .doSelectPageInfo(new ISelect() {
                    @Override
                    public void doSelect() {
                        CopyUtil.copyList(userMapper.selectList(queryWrapper), UserVo.class) ;
                    }
                });
        return userPageInfo;
    }


}
